//  Copyright 2009 David Ekholm, Jalbum AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.package net.jalbum.editor;

package net.jalbum.editor;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationEvent;
import com.apple.eawt.ApplicationListener;

import java.awt.GridLayout;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Rig application for Mac specific behavior
 * @author david
 */
public class SpecialMacHandler implements ApplicationListener {

    /**
     * Do Mac specific stuff in this class
     */
    public SpecialMacHandler() {
        setSystemProperties();
        registerAsApplicationListener(this);
    }

    private static void registerAsApplicationListener(ApplicationListener l) {
        Application app = Application.getApplication();
//        app.removePreferencesMenuItem();
//        app.removeAboutMenuItem();
        app.addApplicationListener(l);

        // Enable stuff
//        app.setEnabledAboutMenu(true);
//        app.setEnabledPreferencesMenu(true);
    }

    private static void setSystemProperties() {
        // Menu settings
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", JTextPad.APP_NAME);

        // Enable selecting directories in filechooser
        System.setProperty("apple.awt.fileDialogForDirectories", "true");

        // Set L&F settings
        System.setProperty("apple.awt.antialiasing", "on");
        System.setProperty("apple.awt.interpolation", "bilinear");

        // Turn off AWT compatibility mode (operations longer than 0.5 sec are interrupted)
        System.setProperty("com.apple.eawt.CocoaComponent.CompatibilityMode", "false");

        // Use quartz graphics pipeline on Leopard to maximize compatibility with Tiger
        System.setProperty("apple.awt.graphics.UseQuartz", "true");
    }

    public void handleOpenApplication(ApplicationEvent applicationEvent) {
        //System.out.println("handleOpenApplication");
    }

    public void handleReOpenApplication(ApplicationEvent applicationEvent) {
    //System.out.println("handleReOpenApplication");
    /*for (JTextPad tp : JTextPad.windows) {
        tp.setVisible(true);
        tp.toFront();
        }*/
    }

    public void handleOpenFile(ApplicationEvent applicationEvent) {
        System.out.println("handleOpenFile");

        File f = new File(applicationEvent.getFilename());
        JTextPad tp = new JTextPad(f);
        tp.setVisible(true);
        applicationEvent.setHandled(true);
    }

    public void handleAbout(ApplicationEvent applicationEvent) {
        applicationEvent.setHandled(true);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1));
        panel.add(new JLabel(JTextPad.APP_NAME + " " + JTextPad.VERSION));
        panel.add(new JLabel("By David Ekholm, Jalbum"));
        panel.add(new JLabel("Thanks to Robert Futrell for providing the excellent"));
        panel.add(new JLabel("text editor engine RSyntaxTextArea!"));
        JOptionPane.showMessageDialog(null, panel, "About " + JTextPad.APP_NAME, JOptionPane.INFORMATION_MESSAGE);
    }

    public void handlePreferences(ApplicationEvent applicationEvent) {
        applicationEvent.setHandled(true);
//        JAlbum.mainFrame.actions.showPreferencesWindowAction.actionPerformed(null);
    }

    public void handlePrintFile(ApplicationEvent applicationEvent) {
        // Empty on purpose
  //      System.out.println("handlePrintFile");
    }

    public void handleQuit(ApplicationEvent applicationEvent) {
    //JTextPad.exitAction.actionPerformed(null);
    }

}
